package com.github.llyb120.calengine.method;

import com.github.llyb120.calengine.entity.ScopeData;
import com.github.llyb120.calengine.rule.RuleEngine;
import groovy.lang.Closure;

import java.util.List;
import java.util.stream.Collectors;

public class CalculateEachFunc extends AbstractScriptFunc{
    public CalculateEachFunc(RuleEngine ruleEngine) {
        super(ruleEngine);
    }

    @Override
    public Object call(Object... args) {
        List<ScopeData> scopeDataList = (List<ScopeData>) args[0];
        Closure func = (Closure) args[1];
        return scopeDataList.stream().map(e -> {
            return ScopeData.builder()
                    .name(e.getName())
                    .data(func.call(e))
                    .build();
        }).collect(Collectors.toList());
    }
}
